#include "composite.h"
#include <iostream>

Component::Component()
{}

Component::~Component()
{}

void Component::Add(Component* com)
{}

void Component::Remove(Component* com)
{}

Component* Component::GetChild(int index)
{
    return NULL;
}

Leaf::Leaf()
{}

Leaf::~Leaf()
{}

void Leaf::Operation()
{
    std::cout << "Leaf::Operation" << std::endl;
}

Composite::Composite()
{
    this->com_vec_.clear();
}

Composite::~Composite()
{}

void Composite::Add(Component* com)
{
    this->com_vec_.push_back(com);
}

void Composite::Remove(Component* com)
{
    for(auto iter = this->com_vec_.begin(); iter != this->com_vec_.end(); ++iter)
    {
        if (*iter == com)
        {
            this->com_vec_.erase(iter);
            return;
        }
    }
}

Component* Composite::GetChild(size_t index)
{
    if (index < 0 || index >= this->com_vec_.size())
    {
        return NULL;
    }
    return this->com_vec_[index];
}

void Composite::Operation()
{
    std::cout << "Composite::Operation" << std::endl;
    for (auto iter = this->com_vec_.begin(); iter != this->com_vec_.end(); ++iter)
    {
        (*iter)->Operation();
    }
}